﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KingerRobot.MexicoSailunMDC.Biz;

internal class LineOutAbnormal : K8.MDCBB.Biz
{
    private const string ClassName = nameof(LineOutAbnormal);

    #region 配置参数
    private ParamData Param { get; set; }

    class ParamData
    {
        /// <summary>
        /// 出库异常口位置id
        /// </summary>
        public long LineLoc { get; set; }
    }
    #endregion

    #region PLC 通讯参数
    private ValueData Data { get; set; }
    class ValueData
    {
        /// <summary>
        /// 产品条码
        /// </summary>
        public string ProductCode { get; set; }
        /// <summary>
        /// 读取任务号
        /// </summary>
        public int WorkNo { get; set; }

    }
    #endregion

    #region 缓存
    private CacheData Cache { get; set; }
    class CacheData
    {

        /// <summary>
        /// 条码
        /// </summary>
        public string ProductCode { get; set; }
        /// <summary>
        /// 读取任务号
        /// </summary>
        public int WorkNo { get; set; }
    }
    #endregion

    #region 实时日志
    private StepData Step { get; set; }
    class StepData
    {
        private string _msg = string.Empty;
        public string StepMsg
        {
            get
            {
                return this._msg;
            }
            set
            {
                if (string.IsNullOrWhiteSpace(value))
                {
                    this._msg = string.Empty;
                    return;
                }
                this._msg = $"{ClassName}|{System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}>>{value}";
            }
        }
    }
    private void StepMsg(string msg)
    {
        this.Step.StepMsg = msg;
        WriteLog(msg);
    }
    #endregion

    #region 文件日志（运行需要 Nlog 配置）
    private void WriteLog(string message)
    {
        K8.LSF.Client.Log(new K8.LSF.Log { Data = message });
    }
    #endregion 
    /// <summary>
    /// 和 PLC 通讯对象
    /// </summary>
    private BizData BizData { get; set; }
    public int Execute(BizData writer)
    {
        this.BizData = writer;
        var result = 0;
        this.StepMsg($"扫描{Data.ProductCode}——开始");


        // 获取条码
        var productCode = Data.ProductCode;
        var taskNo = Data.WorkNo;

        if (Cache.WorkNo == taskNo)
        {
            this.StepMsg($"异常口任务已执行");
            return result;
        }
        if (taskNo == 0)
        {
            this.StepMsg($"异常口任务不存在");
            return result;
        }
        // 条码为空
        if (string.IsNullOrWhiteSpace(productCode) || productCode.StartsWith(ConstFields.Default))
        {
            this.StepMsg($"出库异常口条码为空");
            return result;
        }
        var taskService = K8.RSF.Client.Create<ITaskService>();
        var success = taskService.BakAndDeleteOutAbnormalTaskAndCmdByTaskNo(taskNo);
        if (success > 0)
        {
            Cache.ProductCode = productCode;
            Cache.WorkNo = taskNo;
        }

        return result;

    }
}
